package com.dongdongshop.controller;

import com.dongdongshop.domain.AjaxResult;
import com.dongdongshop.pojo.TbSeller;
import com.dongdongshop.service.OrderService;
import com.dongdongshop.vo.OrderAdminVO;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.poi.hssf.usermodel.*;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import static org.apache.shiro.SecurityUtils.getSubject;

@Controller
@RequestMapping("orderAdmin")
public class OrderController {
    @DubboReference
    private OrderService orderService;

    //查询当前商家的订单信息
    @RequestMapping("findSellerOrderInfo")
    @ResponseBody
    public AjaxResult findSellerOrderInfo(String startDate, String endDate) {
        TbSeller seller = (TbSeller)getSubject().getPrincipal();
        return AjaxResult.OK().setData(orderService.findSellerOrderInfo(startDate, endDate,seller.getSellerId()));
    }

    @RequestMapping(value = "export", method = RequestMethod.GET)
    public void  export(String startDate, String endDate, HttpServletResponse response) {
        TbSeller seller = (TbSeller)getSubject().getPrincipal();
        List<OrderAdminVO> sellerOrderInfo = orderService.findSellerOrderInfo(startDate, endDate, seller.getSellerId());
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        HSSFWorkbook workbook = new HSSFWorkbook();//创建HSSFWorkbook对象,  excel的文档对象
        HSSFSheet sheet = workbook.createSheet("信息表"); //excel的表单


        String fileName = "order-report"+ sdf.format(new Date()) + ".xls";//设置要导出的文件的名字
        //新增数据行，并且设置单元格数据
        int rowNum = 1;
        String[] headers = {"日期", "总金额", "已支付日期", "未支付金额"};
        //headers表示excel表中第一行的表头
        HSSFRow row = sheet.createRow(0);
        //在excel表中添加表头
        for (int i = 0; i < headers.length; i++) {
            HSSFCell cell = row.createCell(i);
            HSSFRichTextString text = new HSSFRichTextString(headers[i]);
            cell.setCellValue(text);
        }

        //在表中存放查询到的数据放入对应的列
        for (OrderAdminVO orderAdminVO : sellerOrderInfo) {
            HSSFRow row1 = sheet.createRow(rowNum);
           // row1.createCell(0).setCellValue(sdf.format(orderAdminVO.getCreateTime()));
            row1.createCell(1).setCellValue(orderAdminVO.getTotalAmount());
            row1.createCell(2).setCellValue(orderAdminVO.getPrepaid());
            row1.createCell(3).setCellValue(orderAdminVO.getUnpaid());
            rowNum++;
        }

        response.setContentType("application/octet-stream");
        response.setHeader("Content-disposition", "attachment;filename=" + fileName);
        try {
            response.flushBuffer();
            workbook.write(response.getOutputStream());
        } catch (IOException e) {
            e.printStackTrace();

        }
    }
}
